/**
 * @title AbsTouzhuMatchListCommunicater.java
 * @package com.esun.cheetah.biz.personal.attention
 * @description 已投注列表的抽象类
 * @author liugan
 * @date 2012-5-8
 * @copyright 深圳市易讯天空
 */
package com.esun.cheetah.biz.personal.attention;

import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import android.os.Handler;
import com.esun.cheetah.biz.personal.personalinfo.UserInfo;
import com.esun.cheetah.config.BizCode;
import com.esun.cheetah.config.Key;
import com.esun.cheetah.config.Language;
import com.esun.cheetah.model.personal.attention.MatchInfo;
import com.esun.cheetah.network.AbsInferiorityCommunicater;
import com.esun.util.log.EDebug;

/**
 * @title AbsTouzhuMatchListCommunicater
 * @description 已投注列表的抽象类
 * @author liugan
 */
public abstract class AbsTouzhuMatchListCommunicater extends
		AbsInferiorityCommunicater {

	/** 已投注比赛信息List */
	private ArrayList<MatchInfo> matchList = new ArrayList<MatchInfo>();

	/**
	 * @param handler
	 */
	public AbsTouzhuMatchListCommunicater(Handler handler) {
		super(handler);
	}

	@Override
	public HashMap<String, String> comHashMap() {
		HashMap<String, String> gainUserAttentionMap = new HashMap<String, String>();
		gainUserAttentionMap.put(Key.KEY_ID, BizCode.BIZCODE_TOUZU_MATCH_LIST);
		gainUserAttentionMap.put(Key.KEY_USERNAME, UserInfo.getUserInfo()
				.getUserName());
		gainUserAttentionMap.put(Key.KEY_CK, UserInfo.getUserInfo().getCk());
		return gainUserAttentionMap;
	}

	@Override
	public void getJsonContent(String serverRes) throws JSONException {
		JSONObject object = (JSONObject) new JSONTokener(serverRes).nextValue();
		JSONArray jsonArray = object.getJSONArray("data");
		matchList.clear();
		// 循环访问每一场比赛的信息
		for (int i = 0; i < jsonArray.length(); i++) {
			JSONObject matchObject = jsonArray.optJSONObject(i);
			EDebug.o(10, matchObject.toString());
			if (matchObject != null) {

				// 返回参数“d”：一场比赛的所有信息，以逗号分割
				// d="WEB对阵ID，对阵ID，比赛日期，比赛时间，
				// 联赛ID，联赛简体名，联赛繁体名，联赛颜色，赛季ID，
				// 主队ID，主队简体名，主队繁体名，主队排名，
				// 客队ID，客队简体名，客队繁体名，客队排名，
				// 当前轮次，阶段名，阶段模式，主队进球数，客队进球数，
				// 主队半场进球，客队半场进球，主队红牌，客队红牌，主队黄牌，客队黄牌"
				String description = (String) matchObject
						.get(Key.KEY_MATCH_DESCRIPTION);
				String[] descriptions = description.split(Language.COMMA);
				MatchInfo matchInfo = new MatchInfo();

				matchInfo.setMatchId(descriptions[1]);// 对阵ID
				matchInfo.setMatchDate(descriptions[2]);// 比赛日期
				matchInfo.setMatchTime(descriptions[3]); // 比赛开始时间
				matchInfo.setLeagueName(descriptions[5]);// 联赛名称
				matchInfo.setRound(descriptions[17]);// 当前轮次
				matchInfo.setStage(descriptions[18]);// 当前阶段
				matchInfo.setHostName(descriptions[10]);// 主队名称
				matchInfo.setHostNumber(descriptions[12]);// 主队排名
				matchInfo.setGuestName(descriptions[14]);// 客队名称
				matchInfo.setGuestNumber(descriptions[16]);// 客队排名
				matchInfo.setHostScore(Integer.parseInt(descriptions[20]));// 主队得分
				matchInfo.setGuestScore(Integer.parseInt(descriptions[21]));// 客队得分

				final int status = matchObject.getInt(Key.KEY_MATCH_STATE);
				final String matchTime = matchObject
						.getString(Key.KEY_MATCH_MATCH_TIME);

				setMatchState(descriptions, matchInfo, status, matchTime);

				// 返回参数：亚指
				final String odds = matchObject.getString(Key.KEY_MATCH_ODDS);
				if ((odds != null) && !odds.equals("")) {
					matchInfo.setOdds(odds);
				} else {
					matchInfo.setOdds(Language.NO_DATA);
				}

				// 返回参数：赔率
				// 格式：pl="3.04,3.22,2.15"
				// “胜，平，负”
				String peilv = matchObject.getString(Key.KEY_MATCH_PEILV);
				if (peilv != null && (!peilv.equalsIgnoreCase(Language.NO_PEILV))) {
					String peilvs[] = peilv.split(Language.COMMA);
					EDebug.o(peilvs.toString());
					matchInfo.setWin(peilvs[0]);// 胜
					matchInfo.setDraw(peilvs[1]);// 平
					matchInfo.setLose(peilvs[2]);// 负
				} else {
					matchInfo.setWin(Language.NO_DATA);// 胜
					matchInfo.setDraw(Language.NO_DATA);// 平
					matchInfo.setLose(Language.NO_DATA);// 负
				}

				matchInfo.setClassifyState(getMatchState());

				matchList.add(matchInfo);
			}
		}
	}

	/**
	 * @title getMatchState
	 * @description
	 * @return
	 */
	protected abstract MatchClassifyState getMatchState();

	public ArrayList<MatchInfo> getMatchList() {
		return matchList;
	}

	/**
	 * @title setMatchStateToday
	 * @description 设置比赛状态
	 * @param descriptions
	 * @param matchInfo
	 * @param status
	 */
	public static void setMatchState(String[] descriptions,
			MatchInfo matchInfo, int status, String matchTime) {
		switch (status) {
		case 0:
			matchInfo.setStateDescription(matchInfo.getMatchDate() + Language.CHANGE_LINE
					+ matchInfo.getMatchTime() + Language.START_MATCH);
			matchInfo.setState(MatchState.notStarted);
			break;
		case 1:
			matchInfo.setStateDescription( Language.FIRST_HALF+ Language.CHANGE_LINE + matchTime + Language.MINUTE);
			matchInfo.setState(MatchState.firstHalf);

			break;
		case 2:
			matchInfo.setStateDescription(Language.HALF_TIME);
			matchInfo.setState(MatchState.halfTime);

			break;
		case 3:
			matchInfo.setStateDescription(Language.SECOND_HALF+Language.CHANGE_LINE + matchTime + Language.MINUTE);
			matchInfo.setState(MatchState.secondHalf);
			break;
		case 4:
			matchInfo.setStateDescription(Language.GAME_OVER);
			matchInfo.setState(MatchState.end);
			break;
		case 5:
			matchInfo.setStateDescription(Language.MATCH_CANCEL);
			matchInfo.setState(MatchState.cancel);
			break;
		case 6:
			matchInfo.setStateDescription(Language.MATCH_CHANGE_TIME);
			matchInfo.setState(MatchState.postpone);
			break;
		case 7:
			matchInfo.setStateDescription(Language.MATCH_ABORT);
			matchInfo.setState(MatchState.abort);
			break;
		case 8:
			matchInfo.setStateDescription(Language.MATCH_INTERRUPT);
			matchInfo.setState(MatchState.interrupt);
			break;
		case 9:
			matchInfo.setStateDescription(Language.MATCH_UP_IN_THE_AIR);
			matchInfo.setState(MatchState.undetermined);

		}
	}

}
